<?php

namespace app\school\controller;
use app\common\controller\BasicAdmin;
use app\common\model\CourseCateModel;
use app\common\model\CourseCommentModel;
use app\common\traits\TableSelect;
use app\common\traits\SelectPage;
use app\common\traits\SelectN;
use think\Model;
use think\db;

class Cameraman extends BasicAdmin
{
    use SelectPage;
	use TableSelect;
	use SelectN;
    /**
     * @var Model
     */
    protected $model = 'CameramanModel';
    protected $title = '摄影师管理';
    public function index(){
        $this->_query($this->model)
            ->like('name')
            ->where(function ($db){
                if($this->request->has('cameraman_cate_id','get') && !empty($this->request->get('cameraman_cate_id'))){
                    $cameraman_cate_id = $this->request->get('cameraman_cate_id');
                    $db->where('cameraman_cate_id',$cameraman_cate_id);
                }
            })
            ->with(['cameramancate'])
            ->page();
    }

    public function works(){
        if($this->request->get('cameraman_id')) {
            $cameraman_id = $this->request->get('cameraman_id');
            $cameraman_name = $this->model->where(['id' => $cameraman_id])->value('name');
            $cameraman_works = CourseCommentModel::where('cameraman_id', $cameraman_id)->select();
            $this->assign('list', $cameraman_works);
            $this->assign('cameraman_id', $cameraman_id);
            $this->assign('cameraman_name', $cameraman_name);
            $this->fetch();
        }
    }


    public function works_form(){

        if ($this->request->isPost()) {
            //提交表单时
            if(empty($this->request->post('id'))){
                //新增作品
                CourseCommentModel::create([
                        'cameraman_id'=>$this->request->post('cameraman_id'),
                        'name'=>$this->request->post('name'),
                        'logo'=>$this->request->post('logo'),
                        'info'=>$this->request->post('info'),
                    ]);
                $this->success('添加作品成功！');
            }else{
                CourseCommentModel::where(['id'=>$this->request->post('id')])
                    ->update([
                        'name'=>$this->request->post('name'),
                        'logo'=>$this->request->post('logo'),
                        'info'=>$this->request->post('info'),
                    ]);
                $this->success('修改作品成功！');
            }
        }
        if($this->request->get('id')) {
            //编辑作品时
            $cameraman_works_id = $this->request->get('id');
            $cameraman_works = CourseCommentModel::where('id', $cameraman_works_id)->find();
            $this->assign('vo', $cameraman_works);
            $this->assign('cameraman_id', $cameraman_works['cameraman_id']);
        }
        if($this->request->get('cameraman_id')){
            //添加作品时
            $cameraman_id = $this->request->get('cameraman_id');
            $this->assign('cameraman_id', $cameraman_id);
        }
        $this->fetch();
    }

    public function _page_filter(&$data){
        $this->cameraman_cate = CourseCateModel::cateTree();
    }

    public function add()
    {
        return $this->_form($this->model, 'form');
    }

    public function _add_form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $this->alterInfo($data);
        }
    }

    public function edit(){
        return $this->_form($this->model,'form');
    }

    public function _edit_form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $this->alterInfo($data);
        }
    }

    protected function alterInfo(&$data){
        if(!isset($data['cameraman_cate_id']) || empty($data['cameraman_cate_id'])){
            $this->error('职称不能留空！');
        }
    }

    public function del()
    {
        return $this->_delete($this->model);
    }

    public function upstatus(){
        $this->_save($this->model, [
            'status' => $this->request->request('status'),
        ],'id', ['id' => $this->request->request('id')]);
    }


    public function del_works(){
        //提交表单时
        if(!empty($this->request->post('id'))){
            CourseCommentModel::where('id','in',$this->request->post('id'))->delete();
            $this->success('删除作品成功！');
        }else{
            $this->success('删除作品失败，请刷新重试！');
        }
    }

    public function upworksstatus(){
        CourseCommentModel::where(['id'=>$this->request->get('id')])->update(['status'=>$this->request->get('status')]);
        $this->success('修改成功！');
    }
}